module pflanza.terrain;

import pflanza.brownterrain;
import std.stream;
import utils.configfile;
import utils.randval;

class Terrain {
    private {
        int mHmapRes;
        RandVali mSize, mOctaves;
        RandValf mRoughness;
        char[] mMaterial;
        BrownTerrain mTerrain;
        char[] mFilename;
    }

    public void fromNode(ConfigNode terrainNode) {
        mHmapRes = terrainNode.getIntValue("hmapres",128);
        mSize = RandVali(terrainNode.getStringValue("size","200"));
        mOctaves = RandVali(terrainNode.getStringValue("noctaves","4"));
        mRoughness = RandValf(terrainNode.getStringValue("roughness","0.5"));
        mMaterial = terrainNode.getStringValue("material","");
    }

    public void generate() {
        mTerrain = new BrownTerrain(mHmapRes,mSize.value,mSize.value/4,mOctaves.value,mRoughness.value);
    }

    public BrownTerrain terrain() {
        return mTerrain;
    }

    public void save(char[] filename) {
        auto terrainFile = new File(filename,FileMode.OutNew);
        mTerrain.saveToStreamRaw(terrainFile);
        terrainFile.close();
        mFilename = filename;
    }

    public void writeScript(Stream st) {
        st.writefln("# Terrain");
        st.writefln("objtri terrain %s",mFilename);
        st.writefln("assignmat %s 0",mMaterial);
        st.writefln("insert terrain");
    }
}
